package com.example.ymm.modules1.controller.user;

import com.example.ymm.annotation.RequestLimiter;
import com.example.ymm.model.CommonResult;
import com.example.ymm.modules1.controller.BaseController;
import com.example.ymm.modules1.service.user.DbUserService;
import com.example.ymm.util.RedisKeyManager;
import com.example.ymm.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/hl/user/userLogin")
@Api(tags = "用户端登录接口")
@Slf4j
public class UserLoginController extends BaseController {
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private DbUserService dbUserService;



    @ApiOperation(value = "钉钉自动登录")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public CommonResult login(@RequestParam("code") String code) {
        try {
            return dbUserService.login(code);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }

    }


    @ApiOperation(value = "钉钉自动登录2")
    @RequestMapping(value = "/login2", method = RequestMethod.POST)
    public CommonResult login2(@RequestParam("code") String code) {
        try {
            return dbUserService.login2(code);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }

    }


    @ApiOperation(value = "用户端退出登录")
    @RequestLimiter(QPS = 1D)
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public CommonResult logout(HttpServletRequest request) {
        try {
            String authHeader = request.getHeader(this.tokenHeader);
            if (authHeader != null && authHeader.startsWith(this.tokenHead)) {
                String authToken = authHeader.substring(this.tokenHead.length());// The part after "Bearer "
                String userId = getUserId();
                redisUtil.remove(RedisKeyManager.user_token_key+userId+":"+authToken);
            }
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.success();
        }


    }


    @ApiOperation(value = "考试系统登录")
    @RequestMapping(value = "/exLogin", method = RequestMethod.POST)
    public CommonResult exLogin(@RequestParam String userName, HttpSession session) {
        try {
            userName=userName.trim();
            return dbUserService.loginByEx(userName,session);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }

    }


}
